import os
import unittest
from datetime import datetime

from XTestRunner import HTMLTestRunner

# 创建测试套件
suite = unittest.TestSuite()

# 加载测试用例
test_loader = unittest.TestLoader()
test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testcases')
suite.addTests(test_loader.discover(test_dir, pattern='test_*.py'))


# 创建测试报告目录
report_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'reports')
if not os.path.exists(report_dir):
    os.makedirs(report_dir)

# 生成测试报告文件
report_file = os.path.join(report_dir, f'report_{datetime.now().strftime("%Y%m%d_%H%M%S")}.html')

# 运行测试并生成报告
with open(report_file, 'wb') as f:
    runner = HTMLTestRunner(
        stream=f,
        title='API自动化测试报告',
        description='基于unittest和requests的接口自动化测试',
        verbosity=2
    )
    runner.run(suite)

print(f"测试报告已生成: {report_file}")